<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Article extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
		$this->load->helper('cookie');
		$this->load->library('Session');
		$this->cklogin();
		$this->load->model('Article_model','article_model');
    }
	
	//分类设置
	public function cate(){
		$data['siderid']=2;
		$data['cate'] = $this->article_model->getcate()->result();
		$nums = $this->getallartnums();
		foreach($data['cate'] as $value){
			$num = $this->getcatearts($value->id);
			$value->num = round(($num/$nums)*100);
		}
		$this->load->view('admin/cate.html',$data);
	}

	//根据分类ID获取分类文章数
	private function getcatearts($id){
		return $this->article_model->getnumforcate($id);
	}
	
	//获取文章总数
	private function getallartnums(){
		return $this->article_model->getallart()->num_rows();
	}

	//发布文章表单
	public function art_form(){
		$data['cate'] = $this->article_model->getcate()->result();
		$data['siderid']=2;
		$this->load->view('admin/addarticle.html',$data);
	}


	//文章提交
	public function addarticle(){
		$data = $this->input->post();
		if(!$data){
			redirect('article/art_form');
			return false;
		}
		$data['uid']=$this->session->userid;
		$res = $this->article_model->addart($data);
		if($res){
			$this->art_form();
		}else{
			echo "添加失败";
		}
	}

	//分类文章
	public function artlist(){
		$data['cate'] = $this->article_model->getcate()->result();
		$data['article'] = $this->article_model->getallart()->result();
		$data['siderid']=2;
		$this->load->view('admin/articlelist.html',$data);
	}

	//编辑文章表单
	public function editarticle($id){
		$data['art'] = $this->article_model->getartbyid($id)->row();
		if(!ckuserlevel()){
			if($data['art']->uid != $this->session->userid){
				echo "<script>alert('您只能修改自己的文章');location.href=history.go(-1);</script>";
				exit;
			}
		}
		$data['cate'] = $this->article_model->getcate()->result();
		$data['siderid']=2;
		$this->load->view('admin/editarticle.html',$data);
	}
	
	//更新文章
	public function updatearticle(){
		$data = $this->input->post();
		if(!$data){
			redirect('article/artlist');
			return false;
		}
		$data['uid']=$this->session->userid;
		$res = $this->article_model->updateart($data);
		if($res){
			echo "更新成功";
		}else{
			echo "更新失败！";
		}
	}

	//删除文章
	public function delete($id){
		$data['art'] = $this->article_model->getartbyid($id)->row();
		if($data['art']->uid != $this->session->userid){
			echo "<script>alert('您只能删除自己的文章');location.href=history.go(-1);</script>";
			exit;
		}

		$res = $this->article_model->deleteart($id);
		if($res){
			echo "<script>location.href=history.go(-1);</script>";	
		}

	}

	//恢复删除文章
	public function revert($id){
		if(!ckuserlevel()){
			echo "<script>alert('你无此权限');location.href=history.go(-1);</script>";
			exit;
		}

		$res = $this->article_model->revert($id);
		if($res){
			echo "<script>location.href=history.go(-1);</script>";	
		}
	}

	//文章查找表单
	public function searchartform(){
		$data['cate'] = $this->article_model->getcate()->result();
		$data['siderid']=2;
		$this->load->view('admin/searchartform.html',$data);
	}

	//文章查找结果
	public function getsearchart(){
		$data = $this->input->post();
		if(!$data){
			redirect('article/searchartform');
			return false;
		}
		$data['article'] = $this->article_model->searchart($data)->result();
		$data['cate'] = $this->article_model->getcate()->result();
		$data['siderid']=2;
		$this->load->view('admin/articlelist.html',$data);
	}
	
	//新增分类
	public function addcate(){
		$name = $this->input->post('name');
		if(!$name){
			redirect('article/cate');
			return false;
		}
		$res = $this->article_model->addcate($name);
		if($res){
			$this->cate();	
		}
	}
	
	//重命名分类
	public function renamecate(){
		if(!ckuserlevel()){
			echo "<script>alert('你无此权限');location.href=history.go(-1);</script>";
			exit;
		}
		$id = $this->input->post('id');
		$name = $this->input->post('name');
		if(!$name or !$id){
			redirect('article/cate');
			return false;
		}
		$res = $this->article_model->renamecate($id,$name);
		if($res){
			$this->cate();	
		}
	}

	//删除分类
	public function deletecate(){
		if(!ckuserlevel()){
			echo "<script>alert('你无此权限');location.href=history.go(-1);</script>";
			exit;
		}
		$id = $this->input->post('id');
		if(!$id){
			redirect('article/cate');
			return false;
		}
		echo $id;
	}

	//回收站
	public function deletelist(){
		$data['cate'] = $this->article_model->getcate()->result();
		$data['article'] = $this->article_model->getalldelart()->result();
		$data['siderid']=2;
		$this->load->view('admin/delartlist.html',$data);
	}
	
	//通用检测登录
	function cklogin(){
		$userid = $this->session->userid;
		if(!$userid){
			redirect('login');
		}
	}


}